/*
silverPDF is sponsored by Aleyant Systems (http://www.aleyant.com)

silverPDF is based on PdfSharp (http://www.pdfsharp.net) and iTextSharp (http://itextsharp.sourceforge.net)

Developers: Ai_boy (aka Oleksii Okhrymenko)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above information and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR SPONSORS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

namespace XForms
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            var fs = Application.GetResourceStream(
                new Uri("XForms;component/Fonts/verdana.ttf", UriKind.RelativeOrAbsolute)).Stream;
            FontPool.Register(
                fs, new string[] { "Verdana" });

            fileStream1 = Application.GetResourceStream(
               new Uri("XForms;component/images/Test.png", UriKind.RelativeOrAbsolute)).Stream;
            fileStream2 = Application.GetResourceStream(
                new Uri("XForms;component/PDFs/SomeLayout.pdf", UriKind.RelativeOrAbsolute)).Stream;
        }

        Stream fileStream1;
        Stream fileStream2;

        private void saveButton1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "PDF file format|*.pdf";

            if (d.ShowDialog() == true)
            {
                // Create a new PDF document
                PdfDocument document = new PdfDocument();

                // Create a font
                XFont font = new XFont("Verdana", 16);

                // Create a new page
                PdfPage page = document.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page, XPageDirection.Downwards);
                //gfx.DrawString("XPdfForm Sample", font, XBrushes.DarkGray, 15, 25, XStringFormat.Default);

                // Step 1: Create an XForm and draw some graphics on it

                // Create an empty XForm object with the specified width and height
                // A form is bound to its target document when it is created. The reason is that the form can 
                // share fonts and other objects with its target document.
                XForm form = new XForm(document, XUnit.FromMillimeter(70), XUnit.FromMillimeter(55));

                // Create an XGraphics object for drawing the contents of the form.
                XGraphics formGfx = XGraphics.FromForm(form);

                // Draw a large transparent rectangle to visualize the area the form occupies
                XColor back = XColors.Orange;
                back.A = 0.2;
                XSolidBrush brush = new XSolidBrush(back);
                formGfx.DrawRectangle(brush, -10000, -10000, 20000, 20000);


                // On a form you can draw...

                // ... text
                formGfx.DrawString("Text, Graphics, Images, and Forms", new XFont("Verdana", 10, XFontStyle.Regular), XBrushes.Navy, 3, 0, XStringFormats.TopLeft);
                XPen pen = XPens.LightBlue.Clone();
                pen.Width = 2.5;

                // ... graphics like Bйzier curves
                formGfx.DrawBeziers(pen, XPoint.ParsePoints("30,120 80,20 100,140 175,33.3"));

                // ... raster images like GIF files
                XGraphicsState state = formGfx.Save();
                formGfx.RotateAtTransform(17, new XPoint(30, 30));
                formGfx.DrawImage(XImage.FromStream(fileStream1), 20, 20);
                formGfx.Restore(state);

                // ... and forms like XPdfForm objects
                state = formGfx.Save();
                formGfx.RotateAtTransform(-8, new XPoint(165, 115));
                formGfx.DrawImage(XPdfForm.FromStream(fileStream2), new XRect(140, 80, 50, 50 * Math.Sqrt(2)));
                formGfx.Restore(state);

                // When you finished drawing on the form, dispose the XGraphic object.
                formGfx.Dispose();


                // Step 2: Draw the XPdfForm on your PDF page like an image

                // Draw the form on the page of the document in its original size
                gfx.DrawImage(form, 20, 50);

                // Draw it stretched
                gfx.DrawImage(form, 300, 100, 250, 40);

                // Draw and rotate it
                int dd = 25;
                for (int idx = 0; idx < 360; idx += dd)
                {
                    gfx.DrawImage(form, 300, 480, 200, 200);
                    gfx.RotateAtTransform(dd, new XPoint(300, 480));
                }
                // Save the document...
                document.Save(d.OpenFile());
            }
        }
    }
}
